/**
 * Created by yangxianming on 2017/9/11.
 */
//加载jmeter脚本列表

//异步请求相关
var xmlhttp;
function loadXMLDoc(mothod, url, par, cfunc)
{
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=cfunc;
    if (mothod == "get"){
        if (par == ""){
            xmlhttp.open("GET", url, true);
        }else{
            xmlhttp.open("GET", url+"?"+par, true);
        }
        xmlhttp.send();
    }
    if (mothod == "post"){
        xmlhttp.open("POST", url, true);
        xmlhttp.send(par);
    }
}

var jmeterResultList;   //保存一份结果列表备份，以便后面点选时数据的获取，避免反复请求服务
function initializeData(){
    //异步请求jmeter结果列表
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    jmeterResultList = r["result"];
                    loadJmeterPage(r["result"]);
                }else{
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("GET", "/jmeter/scriptManager?action=getList", true);
    xmlHttp1.send();

    //得到jmeter node全部地址
    var xmlHttp2 = new XMLHttpRequest();
    xmlHttp2.onreadystatechange = function(){
        if (xmlHttp2.readyState==4){
            if(xmlHttp2.status==200){
                var r = JSON.parse(xmlHttp2.response);
                if (r["apiStatus"] == "1") {
                    showNodeTable(r["result"]);
                    checkNode();
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp2.open("GET", "/jmeter/jmeterNodeManager?action=getNodes", true);
    xmlHttp2.send();
}

//列出项目目录
function setProjectShowList(){
    refreshProjectList(jmeterResultList);
}
//添加项目目录
function addProject(ob){
    var projectName = ob.previousSibling.value;
    var par = "?action=addProject&projectName=" + projectName;
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    jmeterResultList[projectName] = [];
                    refreshProjectList(jmeterResultList);   //刷新项目目录配置页
                    loadJmeterPage(jmeterResultList);       //刷新项目下拉列表
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("GET", "/jmeter/scriptManager" + par, true);
    xmlHttp1.send();
}
//删除一个项目目录
function delProject(ob){
    var projectName = ob.previousSibling.innerText;
    var par = "?action=delProject&projectName=" + projectName;
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    delete jmeterResultList[projectName];
                    refreshProjectList(jmeterResultList);   //刷新项目目录配置页
                    loadJmeterPage(jmeterResultList);       //刷新项目下拉列表
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("GET", "/jmeter/scriptManager" + par, true);
    xmlHttp1.send();
}

//通过jmeterResultList这个数据来刷新id="projectContent"这个项目列表
function refreshProjectList(jmeterResultList){
    var projectContent = document.getElementById("projectContent");
    projectContent.innerText = "";
    var ul = document.createElement("ul");
    ul.setAttribute("style", "margin-bottom: 10px");
    projectContent.appendChild(ul);

    for(var ind in jmeterResultList){
        var innerLi = document.createElement("li");
        var div1 = document.createElement("div");
        var div2 = document.createElement("div");

        div1.setAttribute("style", "width: 100px");
        div1.innerText = ind;
        div2.setAttribute("style", "width: 20px");
        div2.innerText = "-";
        div2.onclick = function(){delProject(this);};
        innerLi.appendChild(div1);
        innerLi.appendChild(div2);
        ul.appendChild(innerLi);
    }

    var lastLi = document.createElement("li");
    var lastInput = document.createElement("input");
    var lastDiv = document.createElement("div");
    lastInput.setAttribute("style", "height: 20px;  margin: 0 !important;  width: 90px !important;");
    lastInput.setAttribute("type", "text;");
    lastDiv.setAttribute("style", "width: 20px;margin-left: 10px");
    lastDiv.innerText = "+";
    lastDiv.onclick = function(){addProject(this);};
    lastLi.appendChild(lastInput);
    lastLi.appendChild(lastDiv);

    ul.appendChild(lastLi);
}

function checkNode(){
    var anodes = document.getElementById("myTab").getElementsByTagName("a");
    recursionCheckNode(0, anodes, anodes.length);
}

function recursionCheckNode(currentIndex, obList, listSize){
    //到达列表尾部就滚粗
    if (currentIndex == listSize){
        return;
    }
    var myHref = obList[currentIndex].getAttribute("href");
    var id = myHref.substring(1);
    //通过递归来循环调用接口
    var par = "?action=checkNode&address=" + obList[currentIndex].innerText;
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    obList[currentIndex].style.color = "green";
                    obList[currentIndex].removeAttribute("highLight");
                    obList[currentIndex].style.backgroundColor = "";
                    //检查一下是否在运行是否要显示齿轮,再做实时检查
                    onceCheckNodeIsRunJmeter(id);
                    checkNodeIsRunJmeter(id);
                }
                if(r["apiStatus"] == "2"){
                    obList[currentIndex].style.backgroundColor = "#F9C6DB";
                    obList[currentIndex].setAttribute("highLight", "");
                }
                if(r["apiStatus"] == "0"){
                    alert(r["errorMsg"])
                }
                recursionCheckNode(currentIndex+1, obList, listSize);
            }
        }
    };
    xmlHttp1.open("GET", "/jmeter/jmeterNodeManager" + par, true);
    xmlHttp1.send();
}

function showNodeTable(jsonData){
    var myTab = document.getElementById("myTab");
    var myTabContent = document.getElementById("myTabContent");
    myTab.innerText = "";
    myTabContent.innerText = "";

    for (var i = 0; i < jsonData.length; i++){
        var timestamp = new Date().getTime();
        var r = Math.floor(Math.random() * (1000 - 1) + 1);
        var li = document.createElement("li");
        var a = document.createElement("a");
        var aX = document.createElement("div");
        var aIMG = document.createElement("img");
        var id = "n"+timestamp+r;
        a.setAttribute("href", "#" + id);
        a.setAttribute("style", "float: left;");
        a.setAttribute("data-toggle", "tab");
        a.innerText = jsonData[i];
        a.onclick = function () {loadMyTabContentPage(this)};
        //a.style.color = "green";
        aX.setAttribute("data-toggle", "tab");
        aX.setAttribute("style", "float: left;width:40px;padding-left: 15px;padding-top: 10px;");
        aX.setAttribute("index", id);
        aX.onclick = function () {removeNode(this)};
        aX.innerText = "X";
        aIMG.setAttribute("style", "float: left;width:40px;margin-top: 1px; display: none");
        aIMG.setAttribute("src", "../image/timg.gif");
        aIMG.setAttribute("index", id);
        li.append(a);
        li.append(aX);
        li.append(aIMG);
        myTab.append(li);

        var div = document.createElement("div");
        div.setAttribute("class", "tab-pane fade");
        div.setAttribute("id", id);
        div.setAttribute("address", jsonData[i]);
        div.innerText = jsonData[i];
        myTabContent.append(div);

    }
    //新增按钮
    addButton();
}

//最后一个+按钮
function addButton(){
    var myTab = document.getElementById("myTab");
    //新增按钮
    var liNew = document.createElement("li");
    var aNew = document.createElement("div");
    aNew.setAttribute("data-toggle", "tab");
    aNew.setAttribute("style", "float: left;padding: 10px;");
    aNew.onclick = function (){clickAddButton()};
    aNew.innerText = "+";
    liNew.append(aNew);
    myTab.append(liNew);
}

function clickAddButton(){
    //点击+号，移除+号，生成输入框
    var myTab = document.getElementById("myTab");
    var myLi = document.getElementById("myTab").getElementsByTagName("li");
    var liLast = myLi[myLi.length-1];
    liLast.innerText = "";

    //新增输入框
    var aNew = document.createElement("input");
    aNew.setAttribute("data-toggle", "tab");
    aNew.setAttribute("class", "jmeterTab");
    aNew.setAttribute("id", "newNode");
    aNew.onblur = function (){addNode()};
    aNew.focus();
    liLast.append(aNew);
    myTab.append(liLast);
}

function addNode(){
    //添加节点接口
    var newNode = document.getElementById("newNode").value.trim();
    if (newNode == ""){
        var myTab = document.getElementById("myTab");
        var myLi = document.getElementById("myTab").getElementsByTagName("li");
        myTab.removeChild(myLi[myLi.length-1]);
        //var liLast = myLi[myLi.length-1];
        //liLast.innerText = "";
        addButton();
        return;
    }

    //异步请求jmeter结果列表
    var par = "?action=addNode&address=" + newNode;
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    var myTab = document.getElementById("myTab");
                    var myLi = document.getElementById("myTab").getElementsByTagName("li");
                    var liLast = myLi[myLi.length-1];
                    liLast.innerText = "";

                    var timestamp = new Date().getTime();
                    var a = document.createElement("a");
                    var aX = document.createElement("div");
                    var aIMG = document.createElement("img");
                    var id = "n"+timestamp;
                    a.setAttribute("href", "#" + id);
                    a.setAttribute("style", "float: left;");
                    a.setAttribute("data-toggle", "tab");
                    a.innerText = newNode;
                    a.onclick =  function () {loadMyTabContentPage(this);};
                    //a.style.color = "green";
                    aX.setAttribute("data-toggle", "tab");
                    aX.setAttribute("style", "float: left;width:40px;padding-left: 15px;padding-top: 10px;");
                    aX.setAttribute("index", id);
                    aX.onclick = function () {removeNode(this)};
                    aX.innerText = "X";
                    aIMG.setAttribute("style", "float: left;width:40px;margin-top: 1px; display: none");
                    aIMG.setAttribute("src", "../image/timg.gif");
                    aIMG.setAttribute("index", id);
                    liLast.append(a);
                    liLast.append(aX);
                    liLast.append(aIMG);
                    myTab.append(liLast);

                    var divList = document.getElementById("myTabContent").getElementsByTagName("div");
                    for (var i = 0; i < divList.length; i++){
                        if (divList[i].classList.contains('active')){
                            var activeIn = document.getElementsByClassName("tab-pane fade active in")[0];
                            activeIn.setAttribute("class", "tab-pane fade");
                        }
                    }

                    var myTabContent = document.getElementById("myTabContent");
                    var div = document.createElement("div");
                    div.setAttribute("class", "tab-pane fade active in");
                    div.setAttribute("id", id);
                    div.setAttribute("address", newNode);
                    //div.innerText = newNode;
                    myTabContent.append(div);

                    loadMyTabContentPage(a);
                    addButton();
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("GET", "/jmeter/jmeterNodeManager" + par, true);
    xmlHttp1.send();
}

function removeNode(ob){
    if(!confirm("确定删除节点？")){
        return;
    }
    var myTab = document.getElementById("myTab");
    var myTabContent = document.getElementById("myTabContent");
    var index = ob.getAttribute("index");
    var deleteAddress = ob.previousSibling.innerText;
    var deleteTabContent = document.getElementById(index);
    myTabContent.removeChild(deleteTabContent);

    myTab.removeChild(ob.parentNode);
    //调取删除接口
    var par = "?action=removeNode&address=" + deleteAddress;
    var xmlHttp2 = new XMLHttpRequest();
    xmlHttp2.onreadystatechange = function(){
        if (xmlHttp2.readyState==4){
            if(xmlHttp2.status==200){
                var r = JSON.parse(xmlHttp2.response);
                if (r["apiStatus"] == "1") {

                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp2.open("GET", "/jmeter/jmeterNodeManager" + par, true);
    xmlHttp2.send();
}

//加载jmeter页面头部的菜单
function loadJmeterPage(jsonData){
    var projectSelect = document.getElementById("project");
    projectSelect.options.length = 0;
    projectSelect.options.add(new Option("全部数据", "all"));

    for (var projectName in jsonData){
        projectSelect.options.add(new Option(projectName, projectName));
    }
}

//点选项目目录列出脚本记录
function projectSelectOnclick(){
    var projectSelect = document.getElementById("project");
    var jmxSelect = document.getElementById("jmx");
    var mySelect = projectSelect.options[projectSelect.selectedIndex].text;
    var mySelectValue = projectSelect.options[projectSelect.selectedIndex].value;

    if (mySelectValue == "all"){
        initializeData()
    }else{
        jmxSelect.options.length = 0;
        jmxSelect.add(new Option("全部数据", "all"));

        for (var i = 0; i < jmeterResultList[mySelect].length; i++){
            var jmxName = jmeterResultList[mySelect][i];
            jmxSelect.add(new Option(jmxName, jmxName));
        }
    }
}

//单传脚本
function singleSendScriptButton(id){
    var projectSelect = document.getElementById("project");
    var selectIndex = projectSelect.selectedIndex;
    var projectValue = projectSelect.options[selectIndex].text;
    var jmxSelect = document.getElementById("jmx");
    var jmxSelectIndex = jmxSelect.selectedIndex;
    var jmxValue = jmxSelect.options[jmxSelectIndex].text;
    var address = document.getElementById(id).getAttribute("address");

    if(selectIndex == 0){
        alert("‘项目目录’还没有选择哦");
        return;
    }
    if(jmxSelectIndex == 0){
        alert("‘脚本列表’还没有选择哦’");
        return;
    }

    jmeterScriptDispense(address, projectValue, jmxValue);
}

//节点发放脚本接口
function jmeterScriptDispense(address, projectName, fileName){
    var data = {
        "address":address,
        "projectName":projectName,
        "fileName":fileName
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"]);
                }
                if (r["apiStatus"] == "1"){
                    alert("ok");
                }
            }
        }
    };
    xmlHttp1.open("POST", "/jmeter/scriptDispense", true);
    xmlHttp1.send(JSON.stringify(data));
}

//加载节点控制页面
function loadMyTabContentPage(ob){
    var id = ob.getAttribute("href").slice(1);
    var controlDiv = document.createElement("div");
    var myTabContent = document.getElementById(id);
    myTabContent.innerText = "";
    //创建上传按钮
    var uploadButton = document.createElement("button");
    uploadButton.innerText = "单传脚本";
    uploadButton.onclick = function (){singleSendScriptButton(id)};
    controlDiv.append(uploadButton);
    //创建日志按钮
    var logButton = document.createElement("button");
    logButton.onclick = function(){showSingleLog(id)};
    logButton.innerText = "展示日志";
    controlDiv.append(logButton);
    //单点启动按钮
    var singleRunButton = document.createElement("button");
    singleRunButton.onclick = function(){runSingleJmeter(id)};
    singleRunButton.innerText = "单点执行";
    controlDiv.append(singleRunButton);

    myTabContent.append(controlDiv);

    //换行
    var br1 = document.createElement("br");
    myTabContent.append(br1);

    //日志div
    var divInTab = document.createElement("div");
    var logUl = document.createElement("ul");
    logUl.setAttribute("id", "contentLog" + id);
    divInTab.append(logUl);
    myTabContent.append(divInTab);
}

//执行全部node的jmeter
function runAllNode(){
    var myTab = document.getElementById("myTab");
    var aList = myTab.getElementsByTagName("a");
    //var ipList = [];
    for (var i=0; i<aList.length; i++){
        if(aList[i].hasAttribute("highlight")){
            continue;
        }
        var id = aList[i].getAttribute("href").slice(1);
        runSingleJmeter(id);
    }
    //alert(ipList);
}

//分发脚本到全部激活节点
function scriptToAllNode(){
    var myTab = document.getElementById("myTab");
    var aList = myTab.getElementsByTagName("a");
    for (var i=0; i<aList.length; i++){
        if(aList[i].hasAttribute("highlight")){
            continue;
        }
        var id = aList[i].getAttribute("href").slice(1);
        singleSendScriptButton(id);
    }
}

//单点执行按钮触发，执行单个node中的jmeter
function runSingleJmeter(id){
    var address = document.getElementById(id).getAttribute("address");
    var jmxSelect = document.getElementById("jmx");
    var jmxValue = jmxSelect.options[jmxSelect.selectedIndex].text;
    if (jmxSelect.selectedIndex == 0){
        alert("请选择“脚本列表”");
        return;
    }
    jmxValue = jmxValue.substr(0, jmxValue.indexOf("."));

    var data = {
        "action":"runNodeJmeter",
        "ip":address,
        "scriptName":jmxValue
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    showLoadingImg(id, "open");
                    checkNodeIsRunJmeter(id);
                }
            }
        }
    };
    xmlHttp1.open("POST", "/jmeter/runJmeterNode", true);
    xmlHttp1.send(JSON.stringify(data));
}

//单次请求 检查jmeter是否在运行(用于页面刚进入时)
function onceCheckNodeIsRunJmeter(id){
    var aList = document.getElementById("myTab").getElementsByTagName("a");
    var tarHref = "#" + id;
    for (var i = 0; i < aList.length; i++){
        if (aList[i].getAttribute("href") == tarHref){
            var ip = aList[i].innerText;
            //var par = "ip=" + ip + "&action=checkJmeter";
            var data = {
                "action":"checkJmeter",
                "ip":ip
            };
            var xmlHttp1 = new XMLHttpRequest();
            xmlHttp1.onreadystatechange = function(){
                if (xmlHttp1.readyState == 4){
                    if (xmlHttp1.status == 200){
                        var r = JSON.parse(xmlHttp1.response);
                        if (r["apiStatus"] == "1") {
                            if (r["result"] == "stop"){
                                showLoadingImg(id, "close");
                            }
                            if (r["result"] == "running"){
                                showLoadingImg(id, "open");
                            }
                        }
                        if(r["apiStatus"] == "2"){
                            showLoadingImg(id, "close");
                        }
                        if(r["apiStatus"] == "0"){
                            alert(r["errorMsg"])
                        }
                    }
                }
            };
            xmlHttp1.open("POST", "/jmeter/runJmeterNode", true);
            xmlHttp1.send(JSON.stringify(data));
            break;
        }
    }
}

//实时请求 检查jmeter是否在运行(用于单点执行后实时请求)
function checkNodeIsRunJmeter(id){
    //每秒检查是否还在运行
    var timer = setInterval(function (){
        var aList = document.getElementById("myTab").getElementsByTagName("a");
        var tarHref = "#" + id;
        for (var i = 0; i < aList.length; i++){
            if (aList[i].getAttribute("href") == tarHref){
                var ip = aList[i].innerText;
                //var par = "ip=" + ip + "&action=checkJmeter";

                var data = {
                    "action":"checkJmeter",
                    "ip":ip
                };

                var xmlHttp1 = new XMLHttpRequest();
                xmlHttp1.onreadystatechange = function(){
                    if (xmlHttp1.readyState == 4){
                        if (xmlHttp1.status == 200){
                            var r = JSON.parse(xmlHttp1.response);
                            if (r["apiStatus"] == "1") {
                                if (r["result"] == "stop"){
                                    clearInterval(timer);
                                    showLoadingImg(id, "close");
                                }
                            }else{
                                clearInterval(timer);
                                showLoadingImg(id, "close");
                            }
                        }else{
                            clearInterval(timer);
                            showLoadingImg(id, "close");
                        }
                    }
                };
                xmlHttp1.open("POST", "/jmeter/runJmeterNode", true);
                xmlHttp1.send(JSON.stringify(data));
                break;
            }
        }
    },1000);
}

//显示和隐藏执行动画
function showLoadingImg(id,op){
    var myTab = document.getElementById("myTab");
    var myTabDivs = myTab.getElementsByTagName("div");
    var divOb;
    var imgOb;
    for (var i=0; i<myTabDivs.length; i++){
        if (myTabDivs[i].getAttribute("index") == id){
            divOb = myTabDivs[i];
            imgOb = divOb.nextSibling;
            break;
        }
    }
    if (op == "open"){
        divOb.style.display = "none";
        imgOb.style.display = "block";
    }
    if (op == "close"){
        divOb.style.display = "block";
        imgOb.style.display = "none";
    }
}

//显示单点服务器的测试日志
function showSingleLog(id){
    var contentLogId = "contentLog" + id;
    var contentLog = document.getElementById(contentLogId);

    var projectSelect = document.getElementById("project");
    var selectIndex = projectSelect.selectedIndex;
    var projectValue = projectSelect.options[selectIndex].value;

    var jmxSelect = document.getElementById("jmx");
    var jmxSelectIndex = jmxSelect.selectedIndex;
    var jmxValue = jmxSelect.options[jmxSelectIndex].value;

    contentLog.innerText = "";
    var address = contentLog.parentNode.parentNode.getAttribute("address");
    //加载日志
    var pro = "&project=" + projectValue;
    var jmx = "&jmx=" + jmxValue;
    var par = "address=" + address + "&action=getNodeLogList" + pro + jmx;
    console.log(par);
    //window.open("/jmeter?" + par + "#");
    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if (xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    for(var i = 0; i < r["result"].length; i++){
                        var jumpUrl = "http://" + address + "/jmeterScript/" + r["result"][i] + "/index.html";
                        var li = document.createElement("li");
                        var aInLi = document.createElement("a");
                        aInLi.setAttribute("href", jumpUrl);
                        aInLi.setAttribute("target", "_blank");
                        aInLi.innerText = r["result"][i];
                        li.append(aInLi);
                        contentLog.append(li);
                    }
                }

            }
        }
    };
    xmlHttp1.open("GET", "/jmeter/jmeterNodeManager?" + par, true);
    xmlHttp1.send();
}

//上传文件弹出层 ob参数一般是this触发这个悬浮层的对象，将更具ob相对位置定位悬浮层位置
function controlTopDiv(whichWindow, mySwitch, ob){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        var left = ob.offsetLeft;
        var top = ob.offsetTop + ob.offsetHeight + 10;
        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
        //设置位置
        var topDiv1001 = topDiv.getElementsByClassName("topDiv1001")[0];
        topDiv1001.style.marginLeft = left + "px";
        topDiv1001.style.marginTop = top + "px";
    }

    if (mySwitch == "close") topDiv.style.display = "none";
}

//保存jmx编辑数据
function saveJmxUserVar(){
    var projectSelect = document.getElementById("project");
    var selectIndex = projectSelect.selectedIndex;
    var projectValue = projectSelect.options[selectIndex].text;
    var jmxSelect = document.getElementById("jmx");
    var jmxSelectIndex = jmxSelect.selectedIndex;
    var jmxValue = jmxSelect.options[jmxSelectIndex].text;

    if(selectIndex == 0){
        alert("‘项目目录’还没有选择哦");
        return;
    }
    if(jmxSelectIndex == 0){
        alert("‘脚本列表’还没有选择哦’");
        return;
    }

    var editInfo = [];
    var tr = document.getElementById("jmxUserVarTable").getElementsByTagName("tbody")[0].getElementsByTagName("tr");
    for(var i = 1; i < tr.length; i++){
        var oneInfo = {};
        var k = tr[i].getElementsByTagName("td")[0].innerText;
        var v = tr[i].getElementsByTagName("td")[1].getElementsByTagName("input")[0].value;
        oneInfo = {"k":k, "v":v};
        editInfo.push(oneInfo);
    }

    //展示用户自定义变量
    var data = {"action":"edit",
        "projectName":projectValue,
        "jmxFileName":jmxValue,
        "editInfo":editInfo};

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if (xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    controlEditJmxTopDiv("showEditJmx", "close", "");
                }else{
                    alert(r["errorMsg"]);
                }

            }
        }
    };
    xmlHttp1.open("POST", "/jmeter/jmxManager", true);
    xmlHttp1.send(JSON.stringify(data));
}

//展示jmx中的用户自定义变量
function showJmxUserVar(){
    var projectSelect = document.getElementById("project");
    var selectIndex = projectSelect.selectedIndex;
    var projectValue = projectSelect.options[selectIndex].text;
    var jmxSelect = document.getElementById("jmx");
    var jmxSelectIndex = jmxSelect.selectedIndex;
    var jmxValue = jmxSelect.options[jmxSelectIndex].text;

    if(selectIndex == 0){
        alert("‘项目目录’还没有选择哦");
        return;
    }
    if(jmxSelectIndex == 0){
        alert("‘脚本列表’还没有选择哦’");
        return;
    }

    //展示用户自定义变量
    var data = {"action":"show",
        "projectName":projectValue,
        "jmxFileName":jmxValue};

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if (xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1") {
                    var tbody = document.getElementById("jmxUserVarTable").getElementsByTagName("tbody")[0];
                    //清空旧数据
                    var firstTR = tbody.getElementsByTagName("tr")[0];
                    tbody.innerText = "";
                    tbody.appendChild(firstTR);
                    //渲染数据
                    for(var i = 0; i < r["result"].length; i++){
                        var k = r["result"][i]["k"];
                        var v = r["result"][i]["v"];
                        var tr = document.createElement("tr");
                        var tdK = document.createElement("td");
                        var tdV = document.createElement("td");
                        var inputV = document.createElement("input");
                        inputV.setAttribute("class", "forTD");
                        tdK.innerText = k;
                        inputV.value = v;
                        tr.appendChild(tdK);
                        tdV.appendChild(inputV);
                        tr.appendChild(tdV);
                        tbody.appendChild(tr);
                    }

                    controlEditJmxTopDiv("showEditJmx", "open", "");
                }else{
                    alert(r["errorMsg"]);
                }
            }
        }
    };
    xmlHttp1.open("POST", "/jmeter/jmxManager", true);
    xmlHttp1.send(JSON.stringify(data));
}

//编辑jmx弹出层
function controlEditJmxTopDiv(whichWindow, mySwitch, ob){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        //显示页面
        topDiv.style.display = "block";
    }

    if (mySwitch == "close") topDiv.style.display = "none";
}

function downloadNS(){
    //window.open('https://gitee.com/grshrd49/TSnode/repository/archive/master.zip')
    try {
        var systemName = navigator.platform;
        if (systemName == "Win32"){
            window.open('/download/TSnode-for-window.zip');
        }else{
            window.open('/download/TSnode-for-mac.zip')
        }
    }catch (err){
        alert("无法获取系统名称，请联系小明去码云下载节点服务");
        window.open('https://gitee.com/grshrd49/TSnode/repository/archive/master.zip');
    }
}


function addJmx(){
    var projectSelect = document.getElementById("project");
    var selectIndex = projectSelect.selectedIndex;
    var projectValue = projectSelect.options[selectIndex].text;
    var fi = document.getElementById('file').files[0];
    var upload = document.getElementById("upload");
    var action = "action=jmeter";

    if(selectIndex == 0){
        alert("需要选择上传到哪个‘项目目录’");
        return;
    }
    if(fi == undefined){
        alert("需要选择上传jmx脚本");
        return;
    }

    //不要猛点
    upload.setAttribute("disabled", "");
    upload.style.color = "red";
    var form = new FormData();
    form.append("file", fi);
    loadXMLDoc("post", "../uploadFile?" + action + "&projectName=" + projectValue, form, null);

    setTimeout(
        function(){
            upload.removeAttribute("disabled");
            upload.style.color = "black";
        }, 5000);
}
